layui.use(['form','layer','upload'],function(){
		$ = layui.jquery;
		form = layui.form;
		layer = layui.layer;

		var upload = layui.upload;
   
		 // 执行上传实例
		var uploadInst = upload.render({
			elem: '#upload_img' //绑定元素
			,url: '/index.php/admins/product/upload_img' //上传接口
			,accept:'images'
			,done: function(res){
				if(res.code>0){
					layer.alert(res.msg,{'icon':2});
					return;
				}
			  //上传完毕回调
			  //$('#pre_img').attr('src',res.msg);
			  var html = '<img src="'+res.msg+'" style="height:30px" />';
			  $('input[name="img"]').after(html);
			  //$('.layui-input-inline').append(html);
			  var values = $('input[name="img"]').val();
			  $('input[name="img"]').val(values+';'+res.msg);
			}
			,error: function(){
			  //请求异常回调
			}
		});

		// select 监听
		form.on('select(cid)', function(data){
			$('.sku-list').html('');
		});

		/*
		var list = [['白色','灰色'],['32G','64G','128G'],['公开版','电信版']];
		$.each(list[0],function(i,v0){$.each(list[1],function(i,v1){$.each(list[2],function(i,v2){
			console.log(v0+' '+v1+' '+v2);
		});});});
		*/
	});

	// 添加属性值
	function add_values(obj){
		var selected = $(obj).parent('div').siblings('div').children('select').val();
		if(selected=='0'){
			layer.alert('请先选择属性名称',{'icon':2});
			return;
		}

		layer.prompt({title: '输入规格属性值，并确认', formType: 0}, function(value,index,ele){
			var html = '<span class="layui-badge-rim"><span class="text">'+$.trim(value)+'</span>\
				<a href="javascript:;" class="del" onclick="del_values(this)">&times;</a></span>';
			$(obj).siblings('div[class="value-list"]').append(html);
			create_values_table();
			layer.close(index);
		});
	}
	// 生成属性表格
	function create_values_table(){
		var select_list = $('.sku-list').find('select');
		// 生成thead
		var thead = create_thead(select_list);
		// 生成结构列表数组
		var list = [];
		$.each(select_list,function(i,v){
			var value_list = $(v).parent('div').siblings('div[class*="layui-word-aux"]').find('span[class="text"]');
			var value_arr = [];
			$.each(value_list,function(ii,vv){
				value_arr.push($(vv).text());
			});
			var obj = new Object;
			obj.property_name = $(v).val();
			obj.property_values = value_arr;
			list.push(obj);
		});

		// 生成tbody
		var tbody = create_tbody(list);

		$('#property_table').html(thead+tbody);
	}

	// 生成表头
	function create_thead(select_list){
		var thead = '<thead><tr>';
		$.each(select_list,function(i,v){
			//var name = $(v).val();
			var text = $(v).find("option:selected").text();
			thead += '<th>'+text+'</th>';
		});
		thead += '<th>价格</th><th>成本</th><th>库存</th>';
		thead += '</tr></thead>';
		return thead;
	}

	// 生成tbody
	function create_tbody(list){
		// list数组类似:[['白色','灰色'],['32G','64G','128G'],['公开版','电信版']];
		var str = '';
		var res = '';
		var tr = '';
		var td = '';

		$.each(list,function(i,v){
			var name_id = list[i].property_name;
			str += '$.each(list['+i+'].property_values,function(i'+i+',v'+i+'){';
			td += '<td name_id='+name_id+'>"+v'+i+'+"</td>';
			if(i==list.length-1){
				td += '<td><input type=\'text\' name=\'price\'></td>';
				td += '<td><input type=\'text\' name=\'cost\'></td>';
				td += '<td><input type=\'text\' name=\'stock\'></td>';
				tr += '<tr>'+td+'</tr>';
			}
		});
		var tbody = 'res +="'+tr+'";';
		str += tbody;
		$.each(list,function(ii,vv){
			str += '});';
		});
		console.log(str);
		eval(str);
		tbody = '<tbody>'+res+'</tbody>';
		return tbody;
	}

	// 添加sku项目
	function add_sku(obj){
		var cid = $('select[name="cid"]').val();
		$.get('/index.php/admins/product/get_sku_view',{'cid':cid},function(res){
			if(res.code>0){
				layer.alert(res.msg,{'icon':2});
			}else{
				$('.sku-list').append(res.data);
				form.render('select');
			}
		},'json');
	}

	// 删除属性值
	function del_values(obj){
		$(obj).parent('span').remove();
		create_values_table();
	}

	// 保存
	function save(){
		var data = new Object;
		// 商品数据
		//data.id = $('input[name="id"]').val();
		data.cid = $('select[name="cid"]').val();
		data.title = $.trim($('input[name="title"]').val());
		data.img = $.trim($('input[name="img"]').val());
		data.keywords = $.trim($('input[name="keywords"]').val());
		data.desc = $.trim($('input[name="desc"]').val());
		data.status = $('input[name="status"]').is(':checked')?1:0;

		// 规格数据
		var sku_list=[];
		var tr_list = $('#property_table tbody').children('tr');
		$.each(tr_list,function(i,v){
			var tr = [];
			var properties = [];
			$.each($(v).children('td'),function(ii,vv){
				var name_id = $(vv).attr('name_id');
				if(name_id!=undefined){
					var obj = new Object;
					obj.property_name_id = name_id;
					obj.property_value = $(vv).text();
					properties.push(obj);
				}
			});

			var obj_stock = new Object;
			obj_stock.price = $.trim($(v).find('input[name="price"]').val());
			obj_stock.cost = $.trim($(v).find('input[name="cost"]').val());
			obj_stock.stock = $.trim($(v).find('input[name="stock"]').val());

			tr.push(obj_stock);
			tr.push(properties);

			//tr.push($(v).attr('sku_id'));
			sku_list.push(tr);
		});
		// 保存商品信息
		data.sku_list = sku_list;

		$.post('/index.php/admins/product/save',data,function(res){
			if(res.code>0){
				layer.msg(res.msg,{'icon':2,'anim':2});
			}else{
				layer.msg(res.msg,{'icon':1});
				//setTimeout(function(){parent.window.location.reload();},1000);
			}
		},'json');
	}